//
//  ZZWelcomeViewController.swift
//  sina
//
//  Created by 张鹏 on 16/5/24.
//  Copyright © 2016年 ZP. All rights reserved.
//

import UIKit
import SDWebImage
class ZZWelcomeViewController: UIViewController {

    override func loadView() {
        //加载视图的时候,加载backView
        view = backView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setUpUI()

    }
    
    override func viewDidAppear(animated: Bool) {
        //开始动画
        startAnimation()
    }
    
    //2. 设置约束
    private func setUpUI() {
        view.addSubview(iconView)
        view.addSubview(nameLable)
    
        //snp: 自动布局
        //设置头像
        iconView.snp_makeConstraints { (make) in
            
            make.bottom.equalTo(self.view).offset(-160)
            make.centerX.equalTo(self.view.snp_centerX)
            
            make.width.equalTo(90)
            make.height.equalTo(90)
 
        }
        
        //设置头像图片并且切圆角
            self.iconView.layer.cornerRadius = 45
            let urlString = ZZUserAccountViewModel.sharedAccountViewModel.userAccount?.avatar_large ?? ""
            let url = NSURL(string: urlString)
            iconView.sd_setImageWithURL(url)

        //设置姓名
        nameLable.snp_makeConstraints { (make) in
            make.top.equalTo(iconView.snp_bottom).offset(16)
            make.centerX.equalTo(iconView.snp_centerX)
        }
    }
    
    
    //3. 设置动画
    private func startAnimation() {
    
        let offset = -(screenHeight - 160)
        UIView.animateWithDuration(1.25, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0.5, options: [], animations: {
        
        //更新约束
        self.iconView.snp_makeConstraints(closure: { (make) in
            
             //TODO: 约束问题??????
            make.bottom.equalTo(offset)
            
        })
        print(self.iconView.frame)
        self.nameLable.snp_makeConstraints(closure: { (make) in
            
        })
        
        //提前刷新父视图
        self.view.layoutIfNeeded()
   
        }) { (_) in
            
            UIView.animateWithDuration(0.25, animations: {
                //
                self.nameLable.alpha = 1
//               self.iconView.layer.cornerRadius = 45
                
                }, completion: { (_) in
                    //
                    print("ok")
                    
                    //发出切换控制器的通知
                    NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewController, object: "main")
            })
        }
    }
    
    
   //1. 懒加载视图
    //背景图片
    private lazy var backView : UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    //头像图片
    private lazy var iconView : UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))

    //名字lable
    private  lazy var nameLable : UILabel  = {
    
        let l = UILabel()
        l.font = UIFont.systemFontOfSize(14)
        l.text = ZZUserAccountViewModel.sharedAccountViewModel.userAccount?.name
        l.textColor = UIColor.darkGrayColor()
        //文字居中
        l.alpha = 0
        //自动布局
        l.sizeToFit()
    
        return l
    }()
    
}


